package com.tinyseed.emcp.api.basicdata.service.impl;

import com.tinyseed.emcp.api.app.common.ListUtil;
import com.tinyseed.emcp.api.basicdata.domain.Enterprise;
import com.tinyseed.emcp.api.basicdata.domain.Workshop;
import com.tinyseed.emcp.api.basicdata.model.EnterpriseInputDTO;
import com.tinyseed.emcp.api.basicdata.model.EnterpriseOutputDTO;
import com.tinyseed.emcp.api.basicdata.repository.EnterpriseRepository;
import com.tinyseed.emcp.api.basicdata.repository.WorkshopRepository;
import com.tinyseed.emcp.api.basicdata.service.EnterpriseService;
import com.tinyseed.hmxx.common.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    private final EnterpriseRepository enterpriseRepository;

    private final WorkshopRepository workshopRepository;

    public EnterpriseServiceImpl(EnterpriseRepository enterpriseRepository, WorkshopRepository workshopRepository) {
        this.enterpriseRepository = enterpriseRepository;
        this.workshopRepository = workshopRepository;
    }

    @Override
    public List<EnterpriseOutputDTO> getList(int size, int current) {
        Pageable pageable = PageRequest.of(current, size);
        /*查找企业*/
        Page<Enterprise> enterpriseList = enterpriseRepository.findAll(pageable);
        /*转换格式为outputdto*/
        List<EnterpriseOutputDTO> enterpriseOutputDTOList = new ArrayList<>();
        enterpriseList.getContent().forEach(temp -> {
            EnterpriseOutputDTO enterpriseOutputDTO1 = new EnterpriseOutputDTO();
            BeanUtils.copyProperties(temp, enterpriseOutputDTO1);
            enterpriseOutputDTOList.add(enterpriseOutputDTO1);
        });
        return enterpriseOutputDTOList;
    }

    @Override
    public List<EnterpriseOutputDTO> getOption() {
        return ListUtil.copy(enterpriseRepository.findAll(),EnterpriseOutputDTO.class);
    }

    @Override
    public Page getList(EnterpriseInputDTO inputDTO, int size, int current) {
        Pageable pageable = PageRequest.of(current - 1, size);
        Page<Enterprise> enterprisePage = enterpriseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, inputDTO, criteriaBuilder), pageable);
        Page<EnterpriseOutputDTO> enterpriseOutputDTOS
                = ListUtil.copyPage(enterprisePage, EnterpriseOutputDTO.class);
        return enterpriseOutputDTOS;
    }

    @Override
    public EnterpriseOutputDTO save(EnterpriseInputDTO request) {
//        if (enterpriseRepository.findByEnterpriseName(request.getEnterpriseName()) != null) {
//            throw new EntityExistException(Enterprise.class,"enterpriseName",request.getEnterpriseName());
//        }

        request.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Enterprise enterprise = enterpriseRepository.save(request.convertToEnterprise());
        if (null != request.getWorkshopList()) {
            request.getWorkshopList().forEach(workshopInputDTO -> {
                Workshop workshop = workshopInputDTO.convertToWorkshop();
                workshop.setEnterpriseId(enterprise.getId());
                workshop.setCreateTime(new Timestamp(System.currentTimeMillis()));
                workshopRepository.save(workshop);
            });
        }
        EnterpriseOutputDTO enterpriseOutputDTO = new EnterpriseOutputDTO();
        BeanUtils.copyProperties(enterprise, enterpriseOutputDTO);
        return enterpriseOutputDTO;
    }

    @Override
    public EnterpriseOutputDTO update(EnterpriseInputDTO request) {
        if (null != request.getWorkshopList()) {
            request.getWorkshopList().forEach(workshopInputDTO -> {
                if (workshopInputDTO.isDeleteFlag()) {
                    workshopRepository.deleteById(workshopInputDTO.getId());
                } else {
                    workshopInputDTO.setEnterpriseId(request.getId());
                    workshopRepository.save(workshopInputDTO.convertToWorkshop());
                }
            });
        }
        Enterprise enterprise = enterpriseRepository.save(request.convertToEnterprise());
        EnterpriseOutputDTO enterpriseOutputDTO = new EnterpriseOutputDTO();
        BeanUtils.copyProperties(enterprise, enterpriseOutputDTO);
        return enterpriseOutputDTO;
    }

    @Override
    public EnterpriseOutputDTO getDataById(Long id) {
        Enterprise enterprise = enterpriseRepository.getOne(id);
        EnterpriseOutputDTO enterpriseOutputDTO = new EnterpriseOutputDTO();
        BeanUtils.copyProperties(enterprise, enterpriseOutputDTO);
        return enterpriseOutputDTO;
    }

    @Override
    public void delete(Long id) {
        workshopRepository.deleteByEnterpriseId(id);
//        enterpriseRepository.deleteById(id);
    }

}
